import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const API_URL = import.meta.env.VITE_API_BASE_URL

export const logOperation = async (operationClass, operationContent) => {
    const authStore = useAuthStore()
    const admin = authStore.getAdmin() || {}
    const operatorAccount = admin.account || '未知用户'
    const operatorName = admin.name || '未知用户'
    try {
        const logData = {
            operatorAccount,
            operatorName,
            operationClass,
            operationContent
            // 不传 operationTime，让后端使用数据库时间
        }
        const { data } = await axios.post(`${API_URL}/v1/operationLog/save`, logData)
        if (data.code !== 2000) {
            ElMessage.error(`操作日志记录失败: ${data.response || '未知错误'}`)
        }
    } catch (error) {
        ElMessage.error('操作日志记录失败')
        console.error('操作日志记录失败:', error.response || error)
    }
}